package com.gzyz.cheetah.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzyz.cheetah.entity.dto.PageDTO;
import com.gzyz.cheetah.entity.table.SensitiveWord;
import com.gzyz.cheetah.mapper.SensitiveWordMapper;
import com.gzyz.cheetah.service.ISensitiveWordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 违禁词 服务实现类
 * </p>
 *
 * @author xclhove
 * @since 2023-11-06
 */
@Service
public class SensitiveWordServiceImpl extends ServiceImpl<SensitiveWordMapper, SensitiveWord> implements ISensitiveWordService {
    
    @Override
    public boolean addSensitiveWord(String sensitiveWord) {
        SensitiveWord sensitiveWords = new SensitiveWord();
        sensitiveWords.setWord(sensitiveWord);
        return this.save(sensitiveWords);
    }
    
    @Override
    public boolean deleteSensitiveWordById(Integer sensitiveId) {
        return this.removeById(sensitiveId);
    }
    
    @Transactional
    @Override
    public boolean deleteSensitiveWordsByIds(List<Integer> sensitiveIds) {
        return this.removeByIds(sensitiveIds);
    }
    
    @Override
    public boolean updateSensitiveWordById(SensitiveWord sensitiveWord) {
        sensitiveWord.setUpdateTime(LocalDateTime.now());
        return this.updateById(sensitiveWord);
    }
    
    @Override
    public SensitiveWord getSensitiveById(Integer sensitiveId) {
        return this.getById(sensitiveId);
    }
    
    @Override
    public PageDTO<SensitiveWord> searchAndPageList(String searchText, Integer pageNumber, Integer pageSize) {
        Page<SensitiveWord> sensitiveWordPage = new Page<>(pageNumber,pageSize);
        LambdaQueryWrapper<SensitiveWord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(!StrUtil.isBlank(searchText),SensitiveWord::getWord,searchText);
        List<SensitiveWord> records = this.page(sensitiveWordPage).getRecords();
        PageDTO<SensitiveWord> pageDTO = new PageDTO<>();
        int total = this.list().size();
        pageDTO.setDataLists(records);
        pageDTO.setTotal(total);
        return pageDTO;
    }
}
